<template>
    <div class="pending_approval">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div">
                        <van-image :src="item?.productImages" class="van-image"
                            @click="enlargeImage(item?.productImages)">
                            <template v-slot:error>加载失败</template>
                            <div class="liveStreamingMechanisms" v-if="item.hmMechanismsList[1]?.cooperationMode">多机制
                            </div>
                        </van-image>
                        <div class="div_right">
                            <div class="div_right_top">
                                <div class="style_name">
                                    <van-text-ellipsis :content="item.styleName" />
                                </div>
                                <div class="auditStatus" :style="auditStatusStyle(item.auditStatus)">
                                    {{ auditStatusNameByIds(item.auditStatus) }}
                                </div>
                            </div>
                            <div class="div_right_center">
                                <van-text-ellipsis rows="1" :content="item.sellingPoints" />
                            </div>
                            <div class="div_right_bottom">
                                <div class="price" v-if="item.cooperationMode == '分佣'">￥ {{
                                    item.price }}</div>
                                <div class="price" v-if="item.cooperationMode == '供货'">￥ {{
                                    item.costPrice }}</div>
                                <div class="costPrice" v-if="item.cooperationMode == '分佣'">{{
                                    item.costPrice }}</div>
                                <div class="commission" v-if='item.commission'>
                                    佣 {{ (item.commission || 0) + "%" }}
                                </div>
                            </div>
                            <div class="div_right_people">
                                <div class="launch">发起人： {{ item.recruiter }}</div>
                                <div class="approvals">审批人：{{ item.auditor }}</div>
                            </div>
                        </div>
                        <div class="div_right_times">
                            <div class="dates">发起时间：{{ item.createTime }}</div>
                            <div class="liveStreamingMechanism">{{ item.liveStreamingMechanism }}
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>
    </div>
</template>
<script setup lang="ts">
import { getPromotionList } from '@/api/approval/index'
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { getEnvValue } from "@/utils/index";
import { showImagePreview } from 'vant';
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
const route = useRoute();

const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);
/** 商品id */
const uniquekey = computed(() => {
    return route.query.id;
});
// 限制连续刷新数据重复问题
const refreFlag = ref(true)

/** 状态颜色 */
const auditStatusStyle = (auditStatus: string) => {
    // 待处理
    if (auditStatus == '0') {
        return {
            'background': '#EBF4FF',
            'color': '#0094FF'
        }
        // 已通过
    } else if (auditStatus == '2') {
        return {
            'background': '#E7FDEE',
            'color': '#19D11B'
        }
        //已驳回
    } else if (auditStatus == '1') {
        return {
            'background': '#FDE7E7',
            'color': '#FC2540'
        }
    }
}
// 核价部
const auditStatu = {
    "0": "待审批",
    "2": "已通过",
    "1": "重新议价",
};
type AuditStatusId = "0" | "1" | "2" | "3";
const auditStatusNameByIds = (id: AuditStatusId): string | undefined => {
    return auditStatu[id] ?? "未知状态";
};
/** 查询商品列表 */
const getList = async () => {
    const res = await getPromotionList({ id: uniquekey.value });
    refreFlag.value = true

    // 空数据 停止加载
    if (res.rows.length == 0) {
        list.value = [];
        finished.value = true;
        return;
    }
    // 有数据 赋值列表 加载状态结束
    res.rows[0].auditLogList.reverse().forEach(async (item) => {
        if (item.productImage) {
            item.productImages = getEnvValue("VITE_APP_PIC_URL") + item.productImage.split(",")[0]
        } else {
            item.productImages = "";
        }
        finished.value = true;
        list.value.push({
            ...res.rows[0],
            ...item,
        });
    });
    loading.value = false;
    refreshing.value = false;
    // 如果list长度大于等于总数据条数，数据全部加载完成
    finished.value = true; //结束加载

};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
/** 加载更多 */
const onLoad = async () => {
    getList();
};
/** 图片放大 */
const enlargeImage = (img: string) => {
    if (img) {
        showImagePreview({
            images: [img],
        });
    }
}
</script>
<style lang="less" scoped>
.pending_approval {
    background-color: #F6FAFF;
    width: 100%;
    height: calc(100vh - 90px);
    border-radius: 5px;
    box-sizing: border-box;

    :deep(.van-image__img) {
        border-radius: 5px !important;
    }
}

.liveStreamingMechanisms {
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 20px;
    background: #0094FF;
    border-radius: 2px 0px 2px 0px;
    font-weight: 400;
    font-size: 12px;
    color: #FFFFFF;
    text-align: center;
    line-height: 20px;
}

.nothing_div {
    width: 100%;
    margin: auto;
    text-align: center;
    margin-top: 20vh;
    font-weight: 400;
    font-size: 12px;
    color: #B8BECA;
    line-height: 12px;
    flex-wrap: wrap;

    .van-image {
        width: 160px;
        height: 160px;
    }

}

.product_list_van_list {
    padding: 0 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    margin-top: 10px;
    background: #f6faff;

    .van-image {
        width: 80px;
        height: 80px;
    }

    &_div {
        width: 100%;
        height: 130px;
        box-sizing: border-box;
        background-color: #fff;
        border-radius: 3px;
        margin-bottom: 8px;
        padding: 5px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        padding: 10px 15px 10px 15px;
        display: flex;
        align-items: center;
    }

    .div_right {
        width: calc(100% - 90px);
        display: flex;
        flex-wrap: wrap;

        .div_right_top {
            margin-top: -5px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            // padding-left: 10px;

            .style_name {
                font-size: 14px;
                font-weight: bold;
                width: 150px;
                display: -webkit-box;
                overflow: hidden;
                text-overflow: ellipsis;
                -webkit-line-clamp: 1;
                -webkit-box-orient: vertical;
            }

            .auditStatus {
                font-size: 14px;
                border-radius: 2px;
                padding: 3px 13px 3px 13px;
                text-align: center;
                border-radius: 3px;
            }

            .stats {
                background: #fde7e7;
                border-radius: 2px;
                padding: 3px 13px 3px 13px;
                font-size: 14px;
                color: #fc2540;
                text-align: center;
                border-radius: 3px;
            }
        }

        .div_right_center {
            // padding-top: 5px;
            margin-top: 5px;
            // padding-left: 10px;
            width: 100%;
            // height: 28px;
            font-size: 12px;
            color: #b8beca;
            display: -webkit-box;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        .div_right_bottom {
            margin-top: 2px;
            margin-bottom: 3px;
            // padding-left: 10px;
            display: flex;
            width: 100%;
            align-items: center;
            justify-content: space-between;

            .price {
                font-size: 12px;
                color: #fc2540;
                font-weight: 500;
                white-space: nowrap;
            }

            .costPrice {
                font-size: 11px;
                color: #a3a3a3;
                font-weight: 500;
                white-space: nowrap;
                margin-left: 10px;
            }

            .commission {
                font-weight: 500;
                font-size: 12px;
                color: #fc2540;
                // border: solid 1px black;
                white-space: nowrap;
                width: 20%;
                margin-left: 20px;
            }

            .liveStreamingMechanism {
                font-size: 11px;
                color: #a3a3a3;
                margin-left: 15px;
                //   width: 20%;
                // text-align: right;
            }

            .anchor {
                margin-left: auto;
                padding-left: 15px;
                font-weight: 400;
                font-size: 12px;
                color: #b8beca;
                //   width: 20%;
                //   border: 1px solid #ffb0b0;
                //   padding: 1px 2px;
                text-align: center;
                white-space: nowrap;
                /* 确保文本在一行内显示 */
                overflow: hidden;
                /* 隐藏溢出的内容 */
                text-overflow: ellipsis;
            }
        }

        .div_right_people {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            // padding-left: 10px;
            width: 100%;
            flex-wrap: wrap;

            .approvals {
                // margin-left: 5px;
                // min-width: 20px;
            }
        }
    }

    .div_right_times {
        width: 100%;
        height: 20px;
        font-size: 12px;
        color: #B8BECA;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
}
</style>
